<template>
  <div class="login_container">
    <el-row>
      <!-- span在屏幕中占的份数一共24份,xs表示视口宽度小于780px的时候所占的份数 -->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginform"
        >
          <h1>Hellow</h1>
          <h2>欢迎来到Family甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model.trim="loginForm.username"
              autofocus
              @keyup.enter="focusInp"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              ref="pwd"
              type="password"
              :prefix-icon="Lock"
              v-model.trim="loginForm.password"
              @keyup.enter="login"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="default" :loading @click="login">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup name="index">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
//引入小仓库
import useUserStore from '@/store/module/user'
//引入vuerouter
import { useRouter, useRoute } from 'vue-router'
//el
import { ElNotification } from 'element-plus'
//提示信息
import { timeTip } from '@/utils/timeTip'
//路由跳转
let $router = useRouter()
//获取路由信息
let $route = useRoute()
const useUser = useUserStore()
//声明定位光标属性
let pwd = ref()
//表单验证
let loginform = ref()
const focusInp = () => pwd.value.focus()
let loginForm = reactive({ username: 'admin', password: '111111' })
let loading = ref(false)
const login = async () => {
  //确定表单验证通过
  await loginform.value.validate()
  loading.value = true
  try {
    //成功跳转
    await useUser.userLogin(loginForm)
    //判断用户退出登录是否携带query参数，有就登录直接跳转
    const redict = $route.query.redirect
    $router.push({ path: (redict as string) || '/' })
    //登录成功提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${timeTip()}`,
    })
  } catch (error) {
    //登录失败提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  } finally {
    loading.value = false
  }
}
//自定义校验规则函数
// rule 校验规则对象
//  value 表单元素文本内容
// callback 规则放行函数：检验成功直接返回函数，失败注入失败提示信息
const vUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 2) {
    callback()
  } else {
    callback(new Error('长度大于5位'))
  }
}
const vPWD = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('长度大于6位'))
  }
}
//表单校验规则
const rules = reactive({
  username: [
    // {
    //   required: true,
    //   min: 5,
    //   max: 11,
    //   message: '长度为3-11位',
    //   trigger: 'blur',
    // },
    {
      trigger: 'change',
      validator: vUserName,
    },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 11,
    //   message: '长度为3-11位',
    //   trigger: 'change',
    // },
    {
      trigger: 'change',
      validator: vPWD,
    },
  ],
})
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    top: 30vh;
    width: 80%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 15px;
    h1 {
      color: whitesmoke;
      font-size: 40px;
      line-height: 50px;
    }
    h2 {
      color: whitesmoke;
      font-size: 20px;
      line-height: 40px;
    }
    button {
      width: 100%;
    }
  }
}
</style>
